#!/bin/bash

host=
FLINK_DIR=/home/wao/flink-1.14.0

if [ "$1" == "script" ]; then
    sha=$(redis-cli -h "$host" script load "$(cat leastKeyAssign.lua)")
    redis-cli -h "$host" set leastKeyScript "$sha"
    sha=$(redis-cli -h "$host" script load "$(cat leastCountAssign.lua)")
    redis-cli -h "$host" set leastCountScript "$sha"
    sha=$(redis-cli -h "$host" script load "$(cat weightAssign.lua)")
    redis-cli -h "$host" set weightScript "$sha"
    sha=$(redis-cli -h "$host" script load "$(cat mergeState.lua)")
    redis-cli -h "$host" set mergeScript "$sha"
    sha=$(redis-cli -h "$host" script load "$(cat atomic.lua)")
    redis-cli -h "$host" set atomicScript "$sha"
    sha=$(redis-cli -h "$host" script load "$(cat aging.lua)")
    redis-cli -h "$host" set agingScript "$sha"
elif [ "$1" == "clean" ]; then
    del=("hash" "module" "leastKey" "leastCount" "range" "weight" "custom")
    for i in "${del[@]}"
    do
        redis-cli -h "$host" del keyByStat_"$i"
    done
    redis-cli -h "$host" del leastKeyAssignMap
    redis-cli -h "$host" del leastCountAssignMap
    redis-cli -h "$host" del leastKeyMap
    redis-cli -h "$host" del leastCountMap
    redis-cli -h "$host" del weightAssignMap
    redis-cli -h "$host" publish keyByChannel clear
elif [ "$1" == "stat" ]; then
    mkdir -p info
    download=("hash" "module" "leastKey" "leastCount" "range" "weight" "custom" "count")
    for i in "${download[@]}"
    do
        redis-cli -h "$host" zrange keyByStat_"$i" 0 -1 withScores > info/"$i"
        python3 sort.py info/"$i"
        rm info/"$i"
    done
elif [ "$1" == "save" ]; then
    task=$("$FLINK_DIR"/bin/flink list | grep 'Low Voltage Detection' | awk -F ' ' '{print $4}')
    path=$("$FLINK_DIR"/bin/flink stop --savepointPath /home/wao/savepoint "$task")
    path=$(echo "$path" | awk -F 'file:' '{print $2}')
    echo "$path" > savepoint
elif [ "$1" == "restore" ]; then
    path=$(cat savepoint)
    "$FLINK_DIR"/bin/flink run -s "$path" -p 60 -d -c iscas.elec.voltage.StreamingJobKeyBy voltage-0.1.jar -k :9092 -r  -u jdbc:dm://133.133.133.86:5236 -n TEST -w 11111111
elif [ "$1" == "cleanState" ]; then
    redis-cli -h "$host" del state
    redis-cli -h "$host" del lastWatermark
    redis-cli -h "$host" del watermark
elif [ "$1" == "cleanCount" ]; then
    redis-cli -h "$host" del keyByStat_count
elif [ "$1" == "calc" ]; then
    max=$(awk 'BEGIN {max = 0} {if ($2+0 > max+0) max = $2} END {print max}' "$3")
    min=$(awk 'BEGIN {min = 100000000} {if ($2+0 < min+0) min = $2} END {print min}' "$3")
    ratio=$(echo "scale=3; $min/$max" | bc)
    echo "$max $min $ratio"
    sum=$(awk '{sum += $2} END {print sum}' "$3")
    balance=$(echo "scale=3; $sum/$2" | bc)
    echo "$sum $balance"
    loss=$(echo "scale=3; ($max-$balance)/$balance" | bc)
    echo "$loss"
elif [ "$1" == "priority" ]; then
    redis-cli -h "$host" del nodes priority_ priority_ priority_
    redis-cli -h "$host" rpush nodes   
    redis-cli -h "$host" rpush priority_   
    redis-cli -h "$host" rpush priority_   
    redis-cli -h "$host" rpush priority_   
elif [ "$1" == "bandwidth" ]; then
    ssh wao@"$2" iperf3 -c "$3" -t 3 -f m | tail -n 4 | head -n 1 | awk '{print $7}'
elif [ "$1" == "latency" ]; then
    ssh wao@"$2" ping -c 5 "$3" | tail -n 9 | head -n 4 | tail -n 3 | awk '{print $7}' | awk -F '=' '{print $2}' | awk '{s += $1} END {printf "%.3f\n", s/NR}'
elif [ "$1" == "tc" ]; then
    interface="ens6f1"
    if [ "$2" == "add" ]; then
        sudo tc qdisc add dev "$interface" root netem delay "$3"ms
    else
        sudo tc qdisc del dev "$interface" root
    fi
elif [ "$1" == "unsubscribe" ]; then
    redis-cli -h "$host" publish stateChannel exit
elif [ "$1" == "setConfig" ]; then
    redis-cli -h "$host" set keyBy"$2" "$3"
elif [ "$1" == "changeConfig" ]; then
    redis-cli -h "$host" publish keyByChannel "$2@$3"
fi
